package com.szbmyoubao.app.ui.test

import android.annotation.SuppressLint
import android.media.MediaMetadataRetriever
import android.media.MediaPlayer
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.base.BaseViewModel
import com.ellecity06.common.ktx.gone
import com.ellecity06.common.ktx.visible
import com.ellecity06.common.utils.logD
import com.ellecity06.cwhiskymall.databinding.ActivityTestBinding

class MainActivity : BaseMvvmActivity<ActivityTestBinding, BaseViewModel>() {
    override fun inflateBinding(): ActivityTestBinding {
        return ActivityTestBinding.inflate(layoutInflater)
    }

    @SuppressLint("SuspiciousIndentation")
    override fun initialize(savedInstanceState: Bundle?) {
        setVideoPath()


    }

    override fun initEvent() {
        binding.shapeCL.setOnClickListener {
            clickIndex = 0
            setSelectState()
        }
        binding.shuzirenIv.setOnClickListener {
            clickIndex = 1
            setSelectState()
        }
        binding.bgIv.setOnClickListener {
            clickIndex = 2
            setSelectState()
        }
    }

    private var clickIndex = -1
    private fun setSelectState() {
        when (clickIndex) {
            // 视频
            0 -> {
                binding.shuzirenIv.isSelected = false
                binding.bgIv.isSelected = false
                binding.shapeCL.isSelected = true
                binding.ivDrag.visible()
                binding.layoutContainer.setSelectedView(binding.textureView)

            }
// 数字人
            1 -> {
                binding.shuzirenIv.isSelected = true
                binding.shapeCL.isSelected = false
                binding.bgIv.isSelected = false
                binding.ivDrag.gone()
                binding.layoutContainer.setSelectedView(binding.shuzirenIv)

            }
// 背景
            2 -> {
                binding.shuzirenIv.isSelected = false
                binding.shapeCL.isSelected = false
                binding.bgIv.isSelected = true
                binding.ivDrag.gone()
                binding.layoutContainer.setSelectedView(binding.bgIv)
            }

            -1 -> {
                binding.shuzirenIv.isSelected = false
                binding.shapeCL.isSelected = false
                binding.ivDrag.gone()
                binding.layoutContainer.setSelectedView(null)

            }
        }
    }

    private val videoFilePath = "/storage/emulated/0/Android/data/com.biboomai.android/files/Movies/VID_20250407150244164.mp4"
    private fun setVideoPath() {
        try {
            getVideoWidthAndHeight(videoFilePath)
            binding.textureView.visible()
            mediaPlayer?.apply {
                pause()
                reset()
                setVolume(0f, 0f)
                // 设置本地视频路径
                setDataSource(videoFilePath)
                prepareAsync()
                setOnPreparedListener {
                    start()
                    setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
                }
                isLooping = true
            }
        } catch (e: Exception) {

        }

    }

    fun getVideoWidthAndHeight(videoPath: String?) {
        val retriever = MediaMetadataRetriever()
        try {
            retriever.setDataSource(videoPath)
            val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
            val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
            // 设置一下宽高
            val layoutParams = binding.textureView.layoutParams as ConstraintLayout.LayoutParams
            layoutParams.dimensionRatio = "${width}:${height}"
            binding.textureView.layoutParams = layoutParams
            retriever.release()
            logD("Width: " + width + ", Height: " + height)
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        }
    }

    private var mediaPlayer: MediaPlayer? = MediaPlayer()

}